package com.xue.qecode.utils;

import com.swetake.util.Qrcode;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class QrCodeUtils {
    public static void createQrCode(String qrData) {
        try {
            Qrcode qrcode = new Qrcode();
            qrcode.setQrcodeErrorCorrect('M');//纠错等级（分为L、M、H三个等级）
            qrcode.setQrcodeEncodeMode('B');//N代表数字，A代表a-Z，B代表其它字符
            qrcode.setQrcodeVersion(7);
            int width = 67 + 12 * (7 - 1);
            int height = 67 + 12 * (7 - 1);
            BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            //绘图
            Graphics2D gs = bufferedImage.createGraphics();
            gs.setBackground(Color.WHITE);
            gs.setColor(Color.BLACK);
            gs.clearRect(0, 0, width, height);
            int pixoff = 2;

            byte[] d = qrData.getBytes("gb2312");
            if (d.length > 0 && d.length < 120) {
                boolean[][] s = qrcode.calQrcode(d);
                for (int i = 0; i < s.length; i++) {
                    for (int j = 0; j < s.length; j++) {
                        if (s[j][i]) {
                            gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3);
                        }
                    }
                }
            }
            String substring = qrData.substring(qrData.length() - 3, qrData.length());

            gs.drawImage(VerifyCode.getimage(substring),50,50,null);
//            gs.setColor(Color.BLUE);
//            gs.setFont(new Font("Menlo",Font.BOLD,30));
//            gs.drawString(substring,,height/2);

            gs.dispose();
            bufferedImage.flush();


            ImageIO.write(bufferedImage, "png", new File("./QrCodeImage/" + qrData + ".png"));
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}
